﻿using System.Linq;
using System.Web.Mvc;
using YogaStudio.Data.Domain;
using YogaStudio.Service.BLL;
using YogaStudio.Utilities;
using YogaStudio.WebUI.Models;

namespace YogaStudio.WebUI.Controllers
{
    [Authorize]
    public class CheckOutController : Controller
    {
        private readonly IOrderService _orderService;
        private readonly IUserManagementService _userManagementService;
        private readonly IShoppingCartService _shoppingCartService;

        public CheckOutController() : this(new OrderService(), new UserManagementService(), new ShoppingCartService()) { }

        public CheckOutController(IOrderService orderService, IUserManagementService userManagementService, IShoppingCartService shoppingCartService)
        {
            _orderService = orderService;
            _userManagementService = userManagementService;
            _shoppingCartService = shoppingCartService;
        }

        public ActionResult BillingAndPayment()
        {
            SetCartsInformaitonInViewBag();
            return (int) ViewBag.TotalCartItems == 0 ? View("EmptyCart") : View();
        }

        [HttpPost]
        public ActionResult BillingAndPayment(PaymentViewModel model)
        {
            if (ModelState.IsValid)
            {
                var currentUser = _userManagementService.GetUser(User.Identity.Name);
                _shoppingCartService.SetCartId(HttpContext);
                var shoppingCartItems = _shoppingCartService.GetCartItems().ToList();

                var paymentDetails = new PaymentDetail
                                     {
                                         PrintedNameOnCard = model.PrintedNameOnCard,
                                         Pin = model.Pin,
                                         Order = null,
                                         BillingAddress1 = model.BillingAddress1,
                                         BillingAddress2 = model.BillingAddress2,
                                         BillingCity = model.BillingCity,
                                         BillingCountry = model.BillingCountry,
                                         BillingPostalCode = model.BillingPostalCode,
                                         BillingProvience = model.BillingProvience,
                                         CreditCardNumber = model.CreditCardNumber,
                                         ExpiryDate = model.ExpiryDate,
                                     };
                var shippingDetails = new Shipping
                                      {
                                          Order = null,
                                          ShipAddress1 = model.ShipAddress1,
                                          ShipAddress2 = model.ShipAddress2,
                                          ShipCity = model.ShipCity,
                                          ShipPostalCode = model.ShipPostalCode,
                                          ShipProvince = model.ShipProvince,
                                          ShipTo = model.ShipTo,
                                          ShippedDate = null,
                                          ShippingStatusId = (int)ShippingStatusEnum.InOrder
                                      };
                _orderService.CreateOrder(currentUser, shoppingCartItems, paymentDetails, shippingDetails);
                _shoppingCartService.EmptyCart();
                return View("PaymentSuccess");
            }

            SetCartsInformaitonInViewBag();
            return View("BillingAndPayment", model);
        }

        private void SetCartsInformaitonInViewBag()
        {
            _shoppingCartService.SetCartId(HttpContext);
            ViewBag.Cart = _shoppingCartService.GetCartItems();
            ViewBag.TotalCartItems = _shoppingCartService.GetCount();
            ViewBag.TotalCartPrice = _shoppingCartService.GetTotal();
        }
	}
}